package com.learn.egcar1.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.learn.egcar1.base.response.JsonResult;
import com.learn.egcar1.base.response.StatusCode;
import com.learn.egcar1.mapper.UserMapper;
import com.learn.egcar1.pojo.dto.UserLoginParam;
import com.learn.egcar1.pojo.dto.UserRegParam;
import com.learn.egcar1.pojo.entity.User;
import com.learn.egcar1.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.spring.web.json.Json;

import javax.servlet.http.HttpSession;
import java.util.Date;

@RestController
@Slf4j
@Api(tags = "用户管理模块")
@RequestMapping("/v1/user/")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    /*
    * 1.注册功能
    * */
    @PostMapping("reg")
    @ApiOperation("01.用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult regUser(@RequestBody @Validated UserRegParam userRegParam){
        /*
        * 1.接收客户端传递来的数据
        * 2.校验数据的合法性
        * 3.确认用户名是否被占用[select接口]
        *  3.1 被占用：return 结束
        *  3.2 没被占用：执行插入[insert接口]，返回；
        * */
        log.debug("userRegParam:"+userRegParam);
        if(userMapper.selectByUsername(userRegParam.getUsername())>0){
            return new JsonResult(StatusCode.USERNAME_EXISTS);
        }

        User user = new User();
        BeanUtils.copyProperties(userRegParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        log.debug("user:"+user);

        int result = userMapper.insertUser(user);
        log.debug(result>1?"注册成功":"注册失败");
        return JsonResult.ok();
    }

    /*
    * 2.登录功能
    * */
    @PostMapping("login")
    @ApiOperation("02.用户登录")
    @ApiOperationSupport(order = 20)
    public JsonResult loginUser(@RequestBody UserLoginParam userLoginParam, @ApiIgnore HttpSession session){
        log.debug("userLoginParam:" +userLoginParam);
        String username = userLoginParam.getUsername();
        String password = userLoginParam.getPassword();
        UserVO userVO = userMapper.selectByUsernameAndPassword(username, password);
        if(userVO==null){
            return new JsonResult(StatusCode.USERNAME_PASSWORD_ERROR);
        }
        //存储会话标识:我们就不把session存在数据库了，存在内存里{“user”:userVO}
        session.setAttribute("user",userVO);

        //登录成功
        return JsonResult.ok(userVO);
    }

    /*
    * 3.获取当前用户信息
    * */
    @GetMapping("currentUser")
    @ApiOperation("03.获取当前用户信息")
    @ApiOperationSupport(order = 30)
    public JsonResult getCurrentUserInfo(@ApiIgnore HttpSession session){
        UserVO userVO = (UserVO) session.getAttribute("user");
        /*if(userVO==null){
            return new JsonResult(StatusCode.NOT_LOGIN);
        }*/

        return JsonResult.ok(userVO);
    }

    /*
    * 4.登出功能
    * */
    @GetMapping("logout")
    @ApiOperation("04.用户登出")
    @ApiOperationSupport(order = 40)
    public JsonResult logoutUser(@ApiIgnore HttpSession session){
        session.removeAttribute("user");
        return JsonResult.ok();
    }
}
